package net.itbaiyang.pattern.email.bridge.mailType;

import net.itbaiyang.pattern.model.EmailDetails;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;

import java.io.File;

public class InlineResourceMail  implements MailStencil {
    private String to;
    private String subject;
    private String content;

    private String rscPath;

    private String rscId;

    public InlineResourceMail(EmailDetails details) {
        this.to = details.getTo();
        this.subject = details.getSubject();
        String content = String.format(details.getContent(), details.getRscId()); // 使用String.format格式化字符串
        this.content = content;
//        this.content = details.getContent();
        this.rscPath = details.getRscPath();
        this.rscId = details.getRscId();

    }

    @Override
    public void apply(MimeMessageHelper helper) throws Exception {
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res); //内嵌静态资源
    }

}
